﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Cat.Domain.Entities;
using Forest.Core;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.WantAds
{
    public partial class WantAdService : IWantAdService
    {
        #region Constants
        private const string WANTAD_BY_ID_KEY = "forest.wantad.id-{0}";
        private const string WANTAD_PATTERN_KEY = "forest.wantads";
        #endregion

        #region Fields
        private readonly IRepository<WantAd> _wantadRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public WantAdService(IRepository<WantAd> wantadRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _wantadRepository = wantadRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }

        #endregion

        #region Base Methods
        public virtual void DeleteWantAd(WantAd wantad)
        {
            if (wantad == null)
                throw new ArgumentNullException("WantAd");

            _wantadRepository.Delete(wantad);
            _cacheManager.RemoveByPattern(WANTAD_PATTERN_KEY);
        }

        public WantAd GetWantAdById(Guid wantadId)
        {
            if (wantadId == Guid.Empty)
                return null;

            string key = string.Format(WANTAD_BY_ID_KEY, wantadId);

            return _cacheManager.Get(key, () =>
            {
                var n = _wantadRepository.GetById(wantadId);
                return n;
            });

        }

        public virtual IPagedList<WantAd> GetAllWantAds(int pageIndex, int pageSize)
        {
            var query = _wantadRepository.Table;

            query = query.OrderBy(w => w.Id);
            var items = new PagedList<WantAd>(query, pageIndex, pageSize);

            return items;
        }

        public virtual void InsertWantAd(WantAd wantad)
        {
            if (wantad == null)
                throw new ArgumentNullException("WantAd");

            _wantadRepository.Insert(wantad);
            _cacheManager.RemoveByPattern(WANTAD_PATTERN_KEY);

            _eventPublisher.EntityInserted(wantad);
        }

        public virtual void UpdateWantAd(WantAd wantad)
        {
            if (wantad == null)
                throw new ArgumentNullException("WantAd");

            _wantadRepository.Update(wantad);
            _cacheManager.RemoveByPattern(WANTAD_PATTERN_KEY);

            _eventPublisher.EntityUpdated(wantad);
        }

        #endregion

        #region Extend Methods

        #region GetHomeWantAds
        public IQueryable<WantAd> GetHomeWantAds(WantAdAreaCode areaCode)
        {
            //var currentDate = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            var currentDate = DateTime.Now.Date;
            var status = (int)Status.Publish;
            return _wantadRepository.Table.Where(x => x.AreaCode == (int)areaCode &&
                //x.StartDate.HasValue &&
                //x.StartDate.Value.CompareTo(currentDate) <= 0 &&
                x.Status.HasValue &&
                x.Status.Value == status &&
                x.EndDate.HasValue &&
                x.EndDate.Value.CompareTo(currentDate) >= 0);
        }
        #endregion

        #endregion
    }
}
